package weaver.interfaces.zhlx.duss.action.financemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 财务收/付款认领流程归档后调用接口流水认领结果到SAP系统
 * @ModifyLog:
 **/
public class WeaCwsfkrllcSapAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaCwsfkrllcSapAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String jbrwb = Util.null2String(rs.getString("jbrwb"));
            //收款
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=? and (sappzh is null or trim(sappzh)='')", mainid);
            int dt1Count = 0;
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                JSONArray yhlsList = new JSONArray();
                JSONArray zjjhList = new JSONArray();
                JSONArray ysxxList = new JSONArray();
                JSONObject dataJson = new JSONObject();
                JSONObject headerJson = new JSONObject();
                headerJson.put("ZLIUCHENGID", lcbh);
                headerJson.put("ZSHOUKUANLEIXING", Util.null2String(rs.getString("lx")));
                headerJson.put("USNAM", jbrwb);
                headerJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
                headerJson.put("KUNNR1", Util.null2String(rs.getString("khdm")));
                headerJson.put("LIFNR", Util.null2String(rs.getString("gysdm")));
                headerJson.put("WAERS", Util.null2String(rs.getString("bz")));
                JSONObject yhlsJson = new JSONObject();
                yhlsJson.put("SERIAL_NO", Util.null2String(rs.getString("yxjylsh")));
                yhlsJson.put("ZWELS", Util.null2String(rs.getString("fkfs")));
                yhlsJson.put("BANKN", Util.null2String(rs.getString("skyxzh")));
                yhlsJson.put("ZFANGXIANG", "0");
                yhlsJson.put("WRBTR2", "");
                yhlsJson.put("BUDAT1", Util.null2String(rs.getString("jyrq")).replace("-", ""));
                yhlsJson.put("WRBTR1", Util.null2String(rs.getString("jyje")));
                yhlsJson.put("RSTGR", Util.null2String(rs.getString("yym")));
                yhlsJson.put("ZUONR", Util.null2String(rs.getString("pjbh")));
                yhlsList.add(yhlsJson);
                headerJson.put("YHLS", yhlsList);
                JSONObject zjjhJson = new JSONObject();
                zjjhJson.put("ZJIHUAID", Util.null2String(rs.getString("zjjhidwb")));
                zjjhJson.put("GJAHR", "");
                zjjhJson.put("BELNR", "");
                zjjhJson.put("BUZEI", "");
                zjjhJson.put("HKONT", Util.null2String(rs.getString("rzkmdm")));
                zjjhJson.put("ZZFZHSLB", Util.null2String(rs.getString("fzhslxbm")));
                zjjhJson.put("ZZFZHSNR", Util.null2String(rs.getString("fzhsnrbm")));
                zjjhJson.put("WRBTR5", "");
                zjjhJson.put("WRBTR3", Util.null2String(rs.getString("jyje")));
                zjjhList.add(zjjhJson);
                headerJson.put("ZJJH", zjjhList);
                JSONObject ysxxJson = new JSONObject();
                ysxxJson.put("VBELN", "");
                ysxxJson.put("HKONT", "");
                ysxxJson.put("WRBTR4", "");
                ysxxList.add(ysxxJson);
                headerJson.put("YSXX", ysxxList);
                dataJson.put("HEADER", headerJson);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaYwskrllcSapAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaCwsfkrllcSapAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("FI012_2", dataJson, dataMap);
                writeLog(">>>>>WeaCwsfkrllcSapAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    String dataStr = outputJson.getString("DATA");
                    JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                    JSONObject returnJson = JSONObject.parseObject(returnDataJson.getString("RETURN"));
                    String belnr = returnJson.getString("MSG_BELNR");
                    String bukrs = returnJson.getString("MSG_BUKRS");
                    String gjahr = returnJson.getString("MSG_GJAHR");
                    String sql = "update " + tablename + "_dt1 set sappzh=?,sapgsdm=?,saphjnd=? where id=?";
                    rs.executeUpdate(sql, belnr, bukrs, gjahr, id);
                    if ("E".equals(msgType)) {
                        dt1Count++;
                        message += outputJson.getString("MSG_TEXT");
                        String mesg = returnJson.getString("MESG");
                        JSONArray mesgList = JSONArray.parseArray(mesg);
                        for (int j = 0; j < mesgList.size(); j++) {
                            String msgType1 = mesgList.getJSONObject(j).getString("MSG_TYPE");
                            if ("E".equalsIgnoreCase(msgType1)) {
                                message += mesgList.getJSONObject(j).getString("MSG_TEXT") + ";";
                            }
                        }
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    dt1Count++;
                }
            }
            //付款
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=? and (sappzbh is null or trim(sappzbh)='')", mainid);
            int dt2Count = 0;
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                JSONObject dataJson = new JSONObject();
                JSONObject headerJson = new JSONObject();
                headerJson.put("ZLIUCHENGID", lcbh);
                headerJson.put("USNAM", jbrwb);
                JSONArray zjjhList = new JSONArray();
                JSONObject zjjhJson = new JSONObject();
                zjjhJson.put("ZJIHUAID", Util.null2String(rs.getString("zjjhxxm")));
                zjjhJson.put("ZZIJINMINGXI", Util.null2String(rs.getString("zjmxxm")));
                zjjhJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
                zjjhJson.put("POSID_EDIT", Util.null2String(rs.getString("wbsh")));
                zjjhJson.put("LIFNR", Util.null2String(rs.getString("gysdm")));
                zjjhJson.put("HKONT", Util.null2String(rs.getString("rzkmdm")));
                zjjhJson.put("BELNR", Util.null2String(rs.getString("lzdjh")));
                zjjhJson.put("GJAHR", Util.null2String(rs.getString("yfpzhjnd")));
                zjjhJson.put("BUZEI", Util.null2String(rs.getString("yfpzxxmbh")));
                zjjhJson.put("RSTGR", Util.null2String(rs.getString("ysbm")));
                zjjhJson.put("ZJIZHANGFANGSHI", Util.null2String(rs.getString("jzfs")));
                zjjhJson.put("ZCONTRACTNO", "");
                zjjhJson.put("EBELN", "");
                zjjhJson.put("WRBTR", "");
                zjjhJson.put("WAERS", Util.null2String(rs.getString("bz")));
                JSONArray yhlsList = new JSONArray();
                JSONObject yhlsJson = new JSONObject();
                yhlsJson.put("SERIAL_NO", Util.null2String(rs.getString("yxjylsh")));
                yhlsJson.put("ZJIHUAID2", Util.null2String(rs.getString("zjjhxxm")));
                yhlsJson.put("ZFUKUANFANGSHI", Util.null2String(rs.getString("fkfs")));
                yhlsJson.put("BANKN", Util.null2String(rs.getString("fkyxzh")));
                yhlsJson.put("ZLIUSHUIFANGXIANG", "1");
                yhlsJson.put("KUNNR", "");
                yhlsJson.put("BUDAT", Util.null2String(rs.getString("jyrq")).replace("-", ""));
                yhlsJson.put("WRBTR", Util.null2String(rs.getString("jyje")));
                yhlsJson.put("WAERS", Util.null2String(rs.getString("bz")));
                yhlsList.add(yhlsJson);
                zjjhJson.put("YHLS", yhlsList);
                zjjhList.add(zjjhJson);
                headerJson.put("ZJJH", zjjhList);
                dataJson.put("HEADER", headerJson);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaCwsfkrllcSapAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaCwsfkrllcSapAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("FI012_1", dataJson, dataMap);
                writeLog(">>>>>WeaCwsfkrllcSapAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    String dataStr = outputJson.getString("DATA");
                    JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                    JSONObject returnJson = JSONObject.parseObject(returnDataJson.getString("RETURN"));
                    JSONArray returnZjjhList = JSONArray.parseArray(returnJson.getString("ZJJH"));
                    for (int i = 0; i < returnZjjhList.size(); i++) {
                        String belnr = returnZjjhList.getJSONObject(i).getString("MSG_BELNR");
                        String bukrs = returnZjjhList.getJSONObject(i).getString("MSG_BUKRS");
                        String gjahr = returnZjjhList.getJSONObject(i).getString("MSG_GJAHR");
                        String sql = "update " + tablename + "_dt2 set sappzbh=?,sapgsdm=?,saphjnd=? where mainid=? and id=?";
                        rs.executeUpdate(sql, belnr, bukrs, gjahr, mainid, id);
                    }
                    if ("E".equalsIgnoreCase(msgType)) {
                        dt2Count++;
                        for (int i = 0; i < returnZjjhList.size(); i++) {
                            String mesg = returnZjjhList.getJSONObject(i).getString("MESG");
                            JSONArray mesgList = JSONArray.parseArray(mesg);
                            for (int j = 0; j < mesgList.size(); j++) {
                                String msgType1 = mesgList.getJSONObject(j).getString("MSG_TYPE");
                                if ("E".equalsIgnoreCase(msgType1)) {
                                    message += mesgList.getJSONObject(j).getString("MSG_TEXT") + ";";
                                }
                            }
                        }
                    }
                } else {
                    dt2Count++;
                    message += Util.null2String(resultJson.getString("message"));
                }
            }

            if (dt1Count > 0 || dt2Count > 0) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaCwsfkrllcSapAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}